package com.daniel.design.pattern.proxy.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @ClassName ProxyFactory
 * @Description 动态代理工厂
 * @Author Daniel
 * @Version V1.0.0
 */
public class ProxyFactory {
    private ISellTickets mTrainStation;

    public ProxyFactory(ISellTickets trainStation) {
        this.mTrainStation = trainStation;
    }

    public ISellTickets getProxyObject() {
        return (ISellTickets) Proxy.newProxyInstance(mTrainStation.getClass().getClassLoader(),
                mTrainStation.getClass().getInterfaces(),
                new MyInvocationHandler(mTrainStation));
    }

    public static class MyInvocationHandler implements InvocationHandler {
        private ISellTickets mISellTickets;

        public MyInvocationHandler(ISellTickets sellTickets) {
            this.mISellTickets = sellTickets;
        }

        @Override
        public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
            System.out.println("动态代理点收取一些费用");
            return method.invoke(mISellTickets, objects);
        }
    }
}
